package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiecImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;
    /*
    * 新增菜品*/
    @Transactional
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 新增菜品的基本信息

        setmealMapper.insert(setmeal);

        Long id =  setmeal.getId();
        // 新增菜品和套餐类的关系信息
        List<SetmealDish> lists = setmealDTO.getSetmealDishes();
        lists.forEach(list ->{
            list.setSetmealId(id);
        });
        setmealDishMapper.insertDish(lists);
    }

    // 套餐的分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 调用PageHelper的方法完成分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        // 调用mapper的方法
        // 调用page的方法
        Long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        // 返回对象
        return new PageResult(total, records);
    }

    /*套餐的批量删除*/
    @Override
    public void deleteBatch(List<Long> ids) {
        // 判断套餐是否为起售状态
        for (Long id : ids){
            Setmeal setmeal = setmealMapper.getId(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                // 此商品在起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        ids.forEach( setmealId ->{
            // 删除菜品和套餐之间的关系
            setmealDishMapper.delete(setmealId);
            // 批量删除套餐
        });
        setmealMapper.deleteBatch(ids);
    }

    @Transactional
    /*修改套餐*/
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 创建一个新的Stemeal对象
        Setmeal setmeal = new Setmeal();
        // 将SetmealDTO中的属性值复制到Setmeal对象中
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 修改套餐的基本信息
        setmealMapper.update(setmeal);

        // 修改套餐关联的菜品信息
        Long id = setmeal.getId();
        // 删除原来的套餐菜品关系
        setmealDishMapper.delete(id);
        // 添加新的套餐菜品关系
        List<SetmealDish> lists = setmealDTO.getSetmealDishes();
        // 设置新的套餐菜品关系
        lists.forEach(setmealDish ->
        {
            setmealDish.setSetmealId(id);
        }
        );
        setmealDishMapper.insertDish(lists);
    }


    /*根据id查询套餐*/
    @Override
    public SetmealVO getById(Long id) {

        //  查询套餐的基本信息
         Setmeal setmeal = setmealMapper.getId(id);
        // 查询套餐关联菜品
        List<SetmealDish> list = setmealDishMapper.getDish(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        // 创建一个Setmeal对象接受
        Setmeal setmeal = setmealMapper.getId(id);
        // 判断套餐内是否含有停售的菜品
        if(setmeal.getStatus()==StatusConstant.DISABLE)
        {
            // 获取里面的菜品
            List<SetmealDish> lists = setmealDishMapper.getDish(id);
            // 判断里面菜品是否有停售的
            lists.forEach(list ->{
               Dish dish = dishMapper.selectById(list.getDishId());
               if(dish.getStatus() == StatusConstant.DISABLE){
                   throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
               }
            });
        }
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);

    }

    /*根据菜品id查询套餐*/
    public List<Setmeal> selectSetmealByCategoryId(Setmeal setmeal) {
        // 根据菜品id查询套餐
        return setmealMapper.selectSetmealByCategoryId(setmeal);
    }

    /*根据套餐ID查询所包含的菜品*/
    public List<DishItemVO> selectDishBySetmealId(SetmealDish setmealDish) {
        List<DishItemVO> list = setmealDishMapper.selectDishBySetmealId(setmealDish);
        return list;
    }
}
